package com.lantu.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.lantu.common.vo.RResult;
import com.lantu.sys.entity.Student;
import com.lantu.sys.mapper.StudentMapper;
import com.lantu.sys.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/student")
@CrossOrigin
public class StudentController {

    @Autowired
    StudentService studentService;
    StudentMapper studentMapper;
    //获取学生列表信息
    @GetMapping("/allStudent/{pageNo}/{pageSize}")
    public RResult getAllStudent(@PathVariable("pageNo") Integer pageNo,
                                @PathVariable("pageSize") Integer pageSize,
                                Student student){
        Page<Student> pageParam = new Page<>(pageNo,pageSize);
        IPage<Student> studentPage = studentService.getStudentByOpr(pageParam, student);
        return RResult.ok(studentPage);
    }
//    根据班级名称进行查询
    @GetMapping("/select/clazzName")
    public RResult selectClazzName(Student student){
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();

        QueryWrapper<Student> clazz_name = studentQueryWrapper.like("clazz_name", student.getClazzName());
        return RResult.ok(clazz_name);

    }

    //增加或者编辑学生信息
    @PostMapping("/addOrUpdateStudent")
    public RResult addOrUpdateStudent(@RequestBody Student student){
        studentService.saveOrUpdate(student);
        return RResult.ok();
    }
    //对表单中学生信息进行回显
    @GetMapping("/{uid}")
    public RResult<Student> getUid(@PathVariable("uid") Integer uid){
        Student byId = studentService.getStudentBuUid(uid);
        return RResult.ok(byId);
    }
    //删除学生信息
    @DeleteMapping("/id/{id}")
    public RResult delStudentById(@PathVariable Integer id
    ){
        studentService.removeById(id);
        return RResult.ok();
    }

    //查询毕业班学生信息
    @GetMapping("/getByClass/{pageNo}/{pageSize}")
    public RResult getByClass(@PathVariable("pageNo") Integer pageNo,
                             @PathVariable("pageSize") Integer pageSize,
                             Student student){
        Page<Student> pageParam = new Page<>(pageNo,pageSize);
        IPage<Student> studentPage = studentService.getByClass(pageParam, student);
        return RResult.ok(studentPage);
    }
    //    首页echarts数据
    @GetMapping("/count/student")
    public List<Map<String,Object>> getstu(){
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        long count = studentService.count(studentQueryWrapper);
        System.out.println(count);

        QueryWrapper<Student> studentQueryWrapper1 = new QueryWrapper<>();
        studentQueryWrapper1.like("clazz_name","七年级");
        long count1 = studentService.count(studentQueryWrapper1);

        QueryWrapper<Student> studentQueryWrapper2 = new QueryWrapper<>();
        studentQueryWrapper2.like("clazz_name","八年级");
        long count2 = studentService.count(studentQueryWrapper2);

        QueryWrapper<Student> studentQueryWrapper3 = new QueryWrapper<>();
        studentQueryWrapper3.like("clazz_name","九年级");
        long count3 = studentService.count(studentQueryWrapper3);

        List<Long> list01 =new ArrayList<>();
        list01.add(count1);
        list01.add(count2);
        list01.add(count3);

        List<String> list02 = new ArrayList<>();
        list02.add("七年级");
        list02.add("八年级");
        list02.add("九年级");

        List<Map<String,Object>> Counst = new ArrayList<>();
        for (int i = 0; i < list01.size(); i++) {
            Map<String,Object> map =new HashMap<>();
            map.put("className",list02.get(i));
            map.put("num",list01.get(i));
            Counst.add(map);
        }
        return Counst;
    }

    @GetMapping("/clazzNameList")
    public List<Map<String,Object>> getClazzNmaeList(){
        List<Student> getlist = studentService.getClassList();
        List<Map<String,Object>> ll = new ArrayList<>();
        for (Student s: getlist
             ) {
            Map<String,Object> map =new HashMap<>();
            if(s != null){
                map.put("label",s.getClazzName());
                map.put("value",s.getClazzName());
                ll.add(map);
            }else{
                System.out.println("hello word");
            }

        }
        return ll;
    }

    //    获取所有学生数
    @GetMapping("/countall")
    public RResult getallcount(){
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.select("count(*)as stunum");
        Student one = studentService.getOne(studentQueryWrapper);
        System.out.println(one);
        return RResult.ok(one);

    }
}
